package com.kolon.business.mResourceRes.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mKolonNews.controller.MResourceResResultController")
public class MResourceResResultController implements SimpleController {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value="/business/mResourceRes/mResourceResResult.do")
	public ModelAndView controller(HttpServletRequest request,
			
		HttpServletResponse response) throws Exception {
		// 도메인
		String url = MsgUtil.getMessage("xml.url");
		// 세션 아이디 및 암호 값을 불러옴
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String id = (String)userMap.get("userId");
		String pw = (String)userMap.get("userPw");
		StringBuffer buffer = new StringBuffer();
		buffer.append(url);
		buffer.append("/resource_res.aspx?mode=").append("6");
		buffer.append("&id=").append(request.getParameter("ArticleCD"));
		buffer.append("&userid=").append(id);
		buffer.append("&pw=").append(pw);
		// XML DOCUMENT 및 인증 처리
		Document doc = ComUtil.getDocument(buffer.toString(), id, pw);
		Element element = doc.getRootElement();
		List xmlList = element.getChildren();
		Iterator itXml = xmlList.iterator();
		Map map = new HashMap();
		// XML 데이터 
		if (itXml.hasNext()) {
			Element el = (Element)itXml.next();
			map = new HashMap();
			map.put("ResourceID", el.getChild("ResourceID").getText());
			map.put("ResourceCode", el.getChild("ResourceCode").getText());
			map.put("Resource_NAME", el.getChild("Resource_NAME").getText());
			map.put("ResourceNMUtf", ComUtil.getEncode(el.getChild("Resource_NAME").getText(),"UTF-8"));
			map.put("StartDate", el.getChild("StartDate").getText().substring(0, 10));
			map.put("year", el.getChild("StartDate").getText().substring(0, 4));
			map.put("month", el.getChild("StartDate").getText().substring(5, 7));
			map.put("day", el.getChild("StartDate").getText().substring(8, 10));
			map.put("EndDate", el.getChild("EndDate").getText().substring(0, 10));
			map.put("StartDateHour", el.getChild("StartDateHour").getText());
			map.put("StartDateMin", el.getChild("StartDateMin").getText());
			map.put("EndDateHour", el.getChild("EndDateHour").getText());
			map.put("EndDateMin", el.getChild("EndDateMin").getText());
			map.put("Contents", el.getChild("Contents").getText().replaceAll("&lt;","<").replaceAll("&gt;",">").replaceAll("&nbsp;"," ").replaceAll("&amp;","&").replaceAll("\n","<br />"));
			map.put("RegUserID", el.getChild("RegUserID").getText());
			map.put("RegDate", el.getChild("RegDate").getText());
			map.put("RepeatYN", el.getChild("RepeatYN").getText());
			map.put("CompanyCD", el.getChild("CompanyCD").getText());
			map.put("ResourceType", el.getChild("ResourceType").getText());
			map.put("ResourceType2", "IT혁신본부 빔프로젝트".equals(el.getChild("Resource_NAME").getText()) ? "DEPT" : el.getChild("ResourceType").getText());
			map.put("MailYN", el.getChild("MailYN").getText());
			map.put("ToUserEmail", el.getChild("ToUserEmail").getText());
			map.put("ToUserInfo", el.getChild("ToUserInfo").getText());
			map.put("Subject", el.getChild("Subject").getText());
			map.put("RegUserDept", el.getChild("RegUserDept").getText());
			map.put("RegUserTitle", el.getChild("RegUserTitle").getText());
			map.put("RegUserName", el.getChild("RegUserName").getText());
			map.put("RegUserInfo", el.getChild("RegUserInfo").getText());
			map.put("revflag", el.getChild("revflag").getText());
		}
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("map", map);
		modelAndView.addObject("ResType", request.getParameter("ResType"));
		modelAndView.addObject("ResTypeCatid", request.getParameter("ResTypeCatid"));
		modelAndView.addObject("ResTypeNM", ComUtil.getEncode(request.getParameter("ResTypeNM"),"UTF-8"));
		return modelAndView;
	}

}